package com.pengtu.web.asset.assets;

import org.apache.commons.lang.StringUtils;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.beans.factory.annotation.Autowired;

import com.pengtu.dao.Page;
import com.pengtu.dao.QueryFilter;
import com.pengtu.entity.asset.office.Information;
import com.pengtu.entity.asset.office.Record;
import com.pengtu.service.asset.AssetsManager;
import com.pengtu.utils.web.Struts2Utils;
import com.pengtu.web.BaseActionSupport;

//覆盖默认的nameSpace
@Namespace("/asset/assets")
// 定义名为reload的result重定向到CarInfomation.action, 其他result则按照convention默认.
@Results({ @Result(name = BaseActionSupport.RELOAD, location = "record.action", type = "redirect") })
public class RecordAction extends BaseActionSupport<Record> {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private AssetsManager assetsManager;

	private String id;
	private Record record;
	private Page<Record> page = new Page<Record>(10);
	private Information assetInfo;

	public Page<Record> getPage() {
		return page;
	}

	public void setId(String id) {
		this.id = id;
	}

	@Override
	public String delete() throws Exception {
		// TODO Auto-generated method stub
		assetsManager.deleteRecord(id);
		addActionMessage("删除资产使用记录!");
		return RELOAD;
	}

	@Override
	public String input() throws Exception {
		// TODO Auto-generated method stub
		if(StringUtils.isNotEmpty(id))
			assetInfo = record.getInformation();
		return INPUT;
	}

	@Override
	public String list() throws Exception {
		QueryFilter queryFilter = QueryFilter.buildFromHttpRequest(Struts2Utils
				.getRequest());
		// 设置默认排序方式
		if (!page.isOrderBySetted()) {
			page.setOrderBy("createDate");
			page.setOrder(Page.DESC);
		}
		page = assetsManager.findPageRecord(page, queryFilter);
		return SUCCESS;
	}

	@Override
	protected void prepareModel() throws Exception {
		if (StringUtils.isNotEmpty(id)) {
			record = assetsManager.getRecord(id);
		} else {
			record = new Record();
		}
	}

	@Override
	public String save() throws Exception {
		// TODO Auto-generated method stub
		String infoId = Struts2Utils.getParameter("infoId");
		assetInfo = assetsManager.getInformation(infoId);
		if(null!=assetInfo)
			record.setInformation(assetInfo);
		assetsManager.saveRecord(record);
		String mes = "";
		if(StringUtils.isNotEmpty(id)){
			mes = "修改";
		}else{
			mes = "新增";
		}
		addActionMessage(mes+"资产记录!");
		return RELOAD;
	}

	@Override
	public Record getModel() {
		// TODO Auto-generated method stub
		return record;
	}
	
	public Information getAssetInfo() {
		return assetInfo;
	}

	public void setAssetInfo(Information assetInfo) {
		this.assetInfo = assetInfo;
	}

	@Autowired
	public void setAssetsManager(AssetsManager assetsManager) {
		this.assetsManager = assetsManager;
	}

}
